﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data.SqlClient;
using System.Data;
using prjKQXS_DLL;

namespace prjKQXS_Report.DMQUY
{
    public partial class rptQuy_Ngay : DevExpress.XtraReports.UI.XtraReport
    {
        //Cài đặt event
        public delegate void OnLoadCompleteHandler();
        public event OnLoadCompleteHandler OnLoadComplete;

        dsReports dsReports1 = new dsReports();

        public rptQuy_Ngay()
        {
            InitializeComponent();
        }

        private void rptdmquy_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            GroupFooter1.Visible = pMaDV.Value.ToString().Trim() == "%";

            lblDonvi.Text = "Phòng XSKT " + pTenDV.Value.ToString().Trim();
            xrlblNgay.Text = "Ngày " + ((DateTime)pNgay.Value).Day.ToString() + 
                " tháng " + ((DateTime)pNgay.Value).Month.ToString() +
                " năm " + ((DateTime)pNgay.Value).Year.ToString();     
       
            Report_DAL report_dal = new Report_DAL();
            dsReports1 = report_dal.reportDungChung(dsReports1);

            dsReports1.Tables.Remove("DMQUY1");
            dsReports1.Tables.Add(xuly());
            this.DataSource = dsReports1;
            this.DataMember = "DMQUY1";
        }   

        private void rptdmquy_AfterPrint(object sender, EventArgs e)
        {
            OnLoadComplete();
        }

        int stt = 0;
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrtcSTT.Text = (++stt).ToString();
        }

        private DataTable xuly()
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            if (conn.State == ConnectionState.Closed)conn.Open();
            string sql = @"Select Ngay
                                , Ngaychungtu
                                , Sophieuthu
                                , Sophieuchi
                                , Noidung
                                , Sotienthu
                                , Sotienchi
                                , Sotienthu- Sotienchi AS Sodu
                                , Madv
                                , D.Ten_DT AS TenDV
                            from DMQUY Q INNER JOIN DMDONVI D ON D.Ma_DT = Q.Madv
                            Where D.isPublic = 1 
                                and rtrim(Madv) like @MaDV and Ngay = @Ngay";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.SelectCommand.Parameters.AddWithValue("@Ngay", ((DateTime)pNgay.Value).Date);
            da.SelectCommand.Parameters.AddWithValue("@MaDV", pMaDV.Value.ToString().Trim());
            DataTable dt = new DataTable();
            da.Fill(dt);
            dt.TableName = "DMQUY1";
            return dt;
        }
    }
}
